package com.ymatou.iapi.productstock.testcase;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productstock.service.ReduceProductStockForOrderCall;
import com.ymatou.iapi.productstock.service.ReturnProductStockForOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductActivityStock;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.ProductTimeStampBean;
import com.ymttest.database.mongo.jproduct.ProductTimeStampWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.JsonBeanFile;

public class Ts_ReturnProductStockForOrder {
	//default data
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static int activityStartId = Integer.parseInt(EnvSetup.getData("productActivityStartId"));
	private static String activityUser = EnvSetup.getData("admin");
	private static String activityPswd = EnvSetup.getData("password");

	private static ReduceProductStockForOrderCall reduceProductStockForOrderCall = new ReduceProductStockForOrderCall();
	private static ReturnProductStockForOrderCall retutnProductStockForOrderCall = new ReturnProductStockForOrderCall();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("返还库存服务");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new ReturnProductStockForOrderCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnProductStockForOrder_001() {
		Logger.start(true, "普通现货商品，不在活动中,返还库存 ");
		Logger.comment("普通现货商品，不在活动中,返还库存 - Test in Ts_ReduceProductStockForOrder->Tc_ReduceProductStockForOrder_001");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnProductStockForOrder_002() {
		Logger.start(true, "普通现货商品，在活动中,返还库存 ");
		Logger.comment("普通现货商品，在活动中,返还库存 - Test in Ts_ReduceProductStockForOrder->Tc_ReduceProductStockForOrder_002");
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnProductStockForOrder_003() {
		Logger.start(true, "FBX商品，不在活动中,返还库存 ");
		Logger.comment("FBX商品，不在活动中,返还库存  - Test in Ts_ReduceProductStockForOrder->Tc_ReduceProductStockForOrder_003");
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnProductStockForOrder_004() {
		Logger.start(true, "FBX商品，在活动中,返还库存");
		Logger.comment("FBX商品，在活动中,返还库存 - Test in Ts_ReduceProductStockForOrder->Tc_ReduceProductStockForOrder_004");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnProductStockForOrder_005() {
		Logger.start(true, "普通商品，在活动中,返还库存");
		try{
			String productId = SellerProductCallService.addNewProductAndApproveActivity(sellerId,
					sellerName,activityUser,activityPswd,String.valueOf(activityStartId),null);
			
			int productInActivityId = new YmtProductsInActivityWapper().selectByProductId(productId).getIproductinactivityid();
			
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogsId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();//共享库存
			
			YmtProductActivityStockWapper ymtActivityStockWapper = new YmtProductActivityStockWapper();
			YmtProductActivityStock ymtActivityStock = ymtActivityStockWapper.selectByProductId(productId).get(0);
			int activityNum = ymtActivityStock.getIstocknum();//活动库存
			
			int BuyerId = Math.abs(TestDataManager.getRandomIntNum(4)); 
			//int OrderId = Math.abs(TestDataManager.getRandomIntNum(8)); 
			int OrderId = getCurrentOrderId();
			int buyNum = -3;
			int returnNum = 3;
		
			
			//时间戳检查
			ProductTimeStampBean productTimeStampBean = new ProductTimeStampBean();
			productTimeStampBean.setProductId(productId);
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				ProductTimeStampWapper productTimeStampWapper = new ProductTimeStampWapper();
				Map<String, Object> ret = productTimeStampWapper.getByProductId(productId);
				productTimeStampBean.setAut((Date)ret.get("aut"));
				productTimeStampBean.setCut((Date)ret.get("cut"));
				productTimeStampBean.setSut((Date)ret.get("sut"));

			}
			
			//===================================================================================================================
			//返还库存
			Logger.comment("返还库存");
			Map<String, Object> returnUpdateMap = new HashMap<String, Object>();
			returnUpdateMap.put("CatalogStockList[0]->ActivityId", activityStartId);
			returnUpdateMap.put("CatalogStockList[0]->BuyerId", BuyerId);
			returnUpdateMap.put("CatalogStockList[0]->CatalogId", catalogsId);
			returnUpdateMap.put("CatalogStockList[0]->CatalogType", 0);
			returnUpdateMap.put("CatalogStockList[0]->OrderId", OrderId);
			returnUpdateMap.put("CatalogStockList[0]->ProductId", productId);
			returnUpdateMap.put("CatalogStockList[0]->SellerId", sellerId);
			returnUpdateMap.put("CatalogStockList[0]->StockNum", returnNum);
			
			JSONObject retutnproductstockBean = JsonBeanHelper.getJsonBean(JsonBeanFile.returnproductstockforordercalltpl);
			JsonBeanHelper.updateJsonBean(retutnproductstockBean, returnUpdateMap);
			retutnProductStockForOrderCall.setData(retutnproductstockBean);
			retutnProductStockForOrderCall.callService();
			Logger.verifyEquals(true, retutnProductStockForOrderCall.getReturnData().contains("ok"), "响应报文-返回码");
			
			
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				ProductTimeStampWapper productTimeStampWapper = new ProductTimeStampWapper();
				NewJavaMongoCheckService.checkActivityProductMgoSyncBySQL_NewJavaMgo(productInActivityId);
				YMTDateUtil.waitTime(5);
				Map<String, Object> dateInMgo = productTimeStampWapper.getByProductId(productId);
				Logger.verifyEquals(true,productTimeStampBean.getSut().equals((Date)dateInMgo.get("sut")),"ProductTimeStamp sut应该没有变化");
				Logger.verifyEquals(true,productTimeStampBean.getCut().equals((Date)dateInMgo.get("cut")),"ProductTimeStamp cut应该没有变化");
				Logger.verifyEquals(false,productTimeStampBean.getAut().equals((Date)dateInMgo.get("aut")),"ProductTimeStamp aut应该变化");
				
			}
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnProductStockForOrder_006() {
		Logger.start(true, "普通商品多规格，在活动中,返还库存");
		try{
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc1.json");
			JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));

			//call&获取返回报文
			String productId = SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
			
			SellerProductCallService.approveActivity(sellerId,activityUser,activityPswd,String.valueOf(activityStartId),productId);
			
			int productInActivityId = new YmtProductsInActivityWapper().selectByProductId(productId).getIproductinactivityid();
			
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogsId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();//共享库存
			
			YmtProductActivityStockWapper ymtActivityStockWapper = new YmtProductActivityStockWapper();
			YmtProductActivityStock ymtActivityStock = ymtActivityStockWapper.selectByProductId(productId).get(0);
			int activityNum = ymtActivityStock.getIstocknum();//活动库存
			
			int BuyerId = Math.abs(TestDataManager.getRandomIntNum(4)); 
			//int OrderId = Math.abs(TestDataManager.getRandomIntNum(8)); 
			int OrderId = getCurrentOrderId();
			int buyNum = -3;
			int returnNum = 3;
		
			//时间戳检查
			ProductTimeStampBean productTimeStampBean = new ProductTimeStampBean();
			productTimeStampBean.setProductId(productId);
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				ProductTimeStampWapper productTimeStampWapper = new ProductTimeStampWapper();
				Map<String, Object> ret = productTimeStampWapper.getByProductId(productId);
				productTimeStampBean.setAut((Date)ret.get("aut"));
				productTimeStampBean.setCut((Date)ret.get("cut"));
				productTimeStampBean.setSut((Date)ret.get("sut"));

			}
			//===================================================================================================================
			//返还库存
			Logger.comment("返还库存");
			Map<String, Object> returnUpdateMap = new HashMap<String, Object>();
			returnUpdateMap.put("CatalogStockList[0]->ActivityId", activityStartId);
			returnUpdateMap.put("CatalogStockList[0]->BuyerId", BuyerId);
			returnUpdateMap.put("CatalogStockList[0]->CatalogId", catalogsId);
			returnUpdateMap.put("CatalogStockList[0]->CatalogType", 0);
			returnUpdateMap.put("CatalogStockList[0]->OrderId", OrderId);
			returnUpdateMap.put("CatalogStockList[0]->ProductId", productId);
			returnUpdateMap.put("CatalogStockList[0]->SellerId", sellerId);
			returnUpdateMap.put("CatalogStockList[0]->StockNum", returnNum);
			
			JSONObject retutnproductstockBean = JsonBeanHelper.getJsonBean(JsonBeanFile.returnproductstockforordercalltpl);
			JsonBeanHelper.updateJsonBean(retutnproductstockBean, returnUpdateMap);
			retutnProductStockForOrderCall.setData(retutnproductstockBean);
			retutnProductStockForOrderCall.callService();
			Logger.verifyEquals(true, retutnProductStockForOrderCall.getReturnData().contains("ok"), "响应报文-返回码");
			

			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				ProductTimeStampWapper productTimeStampWapper = new ProductTimeStampWapper();
				NewJavaMongoCheckService.checkActivityProductMgoSyncBySQL_NewJavaMgo(productInActivityId);
				YMTDateUtil.waitTime(5);
				Map<String, Object> dateInMgo = productTimeStampWapper.getByProductId(productId);
				Logger.verifyEquals(true,productTimeStampBean.getSut().equals((Date)dateInMgo.get("sut")),"ProductTimeStamp sut应该没有变化");
				Logger.verifyEquals(true,productTimeStampBean.getCut().equals((Date)dateInMgo.get("cut")),"ProductTimeStamp cut应该没有变化");
				Logger.verifyEquals(false,productTimeStampBean.getAut().equals((Date)dateInMgo.get("aut")),"ProductTimeStamp aut应该变化");
				
			}
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnProductStockForOrder_007() {
		Logger.start(true, "普通商品,返还库存");
		try{
			//Test date
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			
			String catalogsId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int BuyerId = Math.abs(TestDataManager.getRandomIntNum(4)); 
			int OrderId = Math.abs(TestDataManager.getRandomIntNum(8)); 
			int buyNum = -2;
			int returnNum = 2;
			
			//时间戳检查
			ProductTimeStampBean productTimeStampBean = new ProductTimeStampBean();
			productTimeStampBean.setProductId(productId);
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				ProductTimeStampWapper productTimeStampWapper = new ProductTimeStampWapper();
				Map<String, Object> ret = productTimeStampWapper.getByProductId(productId);
				productTimeStampBean.setCut((Date)ret.get("cut"));
				productTimeStampBean.setSut((Date)ret.get("sut"));

			}
		
			
			
			//===================================================================================================================
			//返还库存
			Logger.comment("返还库存");
			Map<String, Object> returnUpdateMap = new HashMap<String, Object>();
			returnUpdateMap.put("CatalogStockList[0]->ActivityId", 0);
			returnUpdateMap.put("CatalogStockList[0]->BuyerId", BuyerId);
			returnUpdateMap.put("CatalogStockList[0]->CatalogId", catalogsId);
			returnUpdateMap.put("CatalogStockList[0]->CatalogType", 0);
			returnUpdateMap.put("CatalogStockList[0]->OrderId", OrderId);
			returnUpdateMap.put("CatalogStockList[0]->ProductId", productId);
			returnUpdateMap.put("CatalogStockList[0]->SellerId", sellerId);
			returnUpdateMap.put("CatalogStockList[0]->StockNum", returnNum);
			
			JSONObject retutnproductstockBean = JsonBeanHelper.getJsonBean(JsonBeanFile.returnproductstockforordercalltpl);
			JsonBeanHelper.updateJsonBean(retutnproductstockBean, returnUpdateMap);
			retutnProductStockForOrderCall.setData(retutnproductstockBean);
			retutnProductStockForOrderCall.callService();
			Logger.verifyEquals(true, retutnProductStockForOrderCall.getReturnData().contains("ok"), "响应报文-返回码");
			
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductStockMgoSyncBySQL_NewJavaMgo(productId);
				
				ProductTimeStampWapper productTimeStampWapper = new ProductTimeStampWapper();
				YMTDateUtil.waitTime(5);
				Map<String, Object> dateInMgo = productTimeStampWapper.getByProductId(productId);
				Logger.verifyEquals(true,productTimeStampBean.getSut().equals((Date)dateInMgo.get("sut")),"ProductTimeStamp sut应该没有变化");
				Logger.verifyEquals(false,productTimeStampBean.getCut().equals((Date)dateInMgo.get("cut")),"ProductTimeStamp cut应该变化");
					
			}
				
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnProductStockForOrder_008() {
		Logger.start(true, "普通多规格商品,返还库存");
		try{
			
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc10.json");	
			JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));

			//call&获取返回报文
			String productId = SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
			
			
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			
			String catalogsId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int BuyerId = Math.abs(TestDataManager.getRandomIntNum(4)); 
			int OrderId = Math.abs(TestDataManager.getRandomIntNum(8)); 
			int buyNum = -2;
			int returnNum = 2;
			
		
		
			
			
			//===================================================================================================================
			//返还库存
			Logger.comment("返还库存");
			Map<String, Object> returnUpdateMap = new HashMap<String, Object>();
			returnUpdateMap.put("CatalogStockList[0]->ActivityId", 0);
			returnUpdateMap.put("CatalogStockList[0]->BuyerId", BuyerId);
			returnUpdateMap.put("CatalogStockList[0]->CatalogId", catalogsId);
			returnUpdateMap.put("CatalogStockList[0]->CatalogType", 0);
			returnUpdateMap.put("CatalogStockList[0]->OrderId", OrderId);
			returnUpdateMap.put("CatalogStockList[0]->ProductId", productId);
			returnUpdateMap.put("CatalogStockList[0]->SellerId", sellerId);
			returnUpdateMap.put("CatalogStockList[0]->StockNum", returnNum);
			
			JSONObject retutnproductstockBean = JsonBeanHelper.getJsonBean(JsonBeanFile.returnproductstockforordercalltpl);
			JsonBeanHelper.updateJsonBean(retutnproductstockBean, returnUpdateMap);
			retutnProductStockForOrderCall.setData(retutnproductstockBean);
			retutnProductStockForOrderCall.callService();
			Logger.verifyEquals(true, retutnProductStockForOrderCall.getReturnData().contains("ok"), "响应报文-返回码");
			
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductStockMgoSyncBySQL_NewJavaMgo(productId);
			}
				
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	
	
	
	

	//获取当前日期订单号
	private static int getCurrentOrderId() throws InterruptedException{
		YmtOrdersWapper ymtOrdersWapper = new YmtOrdersWapper();
		int iOrder = ymtOrdersWapper.selectEarlyOrderInfo().getIorderid();
		ymtOrdersWapper.updateOrderAddtimeByOrderId(iOrder, 0);
		
		return iOrder;
	}
	
	
	public static void main(String[] args) {
		new Thread(()->{});
	}
	
}
